﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using CellarDoor.Gui;
using CellarDoor.Gui.Elements;
using CellarDoor.Gui.Animation;

namespace CellarDoor.Gui.Demo.DemoScreens
{
    public class BaseScreen : UiFrame
    {
        protected UiPanel Panel;
        protected UiButton ButtonExit;

        public BaseScreen(UiManager manager)
            : base(manager)
        {
            Initialize();
        }

        private void Initialize()
        {
            this.Style.ElementName = "Background";

            Panel = new UiPanel(this);
            Panel.Width = 600;
            Panel.Height = 400;
            Panel.Top = 100;
            Panel.Left = 100;
            Panel.Align = UiAlignStyle.Middle;
            // let the panel use both scale axis for scaling
            Panel.Layout.SizeScaleAxis = UiScaleAxis.Both;
            Panel.Layout.PositionScaleAxis = UiScaleAxis.Both;

            ButtonExit = new UiButton(Panel);
            ButtonExit.Width = 100;
            ButtonExit.Height = 35;
            ButtonExit.Margin = new UiPadding(5);
            // let the button stick on the bottom right in the panel
            ButtonExit.Align = UiAlignStyle.OutsideBottom | UiAlignStyle.Right;
            ButtonExit.Click += (s, e) => this.Close();
            ButtonExit.Text = "Close";
        }

        public override void Show()
        {
            base.OnShowBefore();
            base.PerformShow();

            Vector2 targetPosition = new Vector2(100, 100);

            // disable align mode, so the animation component is able to change the position
            Panel.Align = UiAlignStyle.None;
            // shift panel to start position
            Panel.Top = -Panel.Height;
            // start slide animation to target position
            Panel.SlideTo(targetPosition, 1000, EaseFunction.OutBounce).StopAnimation += (s) =>
            {
                // enable align mode again, so the panel stays in the center on each resolution
                Panel.Align = UiAlignStyle.Middle;
            };

            // make panel invisible
            Panel.Opacity = 0;
            // start fade in animation
            Panel.FadeTo(1, 1000).StopAnimation += (s) =>
            {
                this.Clickable = true;
                base.OnShowAfter();
            };   
        }

        public override void Close()
        {
            this.Clickable = false;
            base.OnCloseBefore();

            // disable align mode, so slide animation
            Panel.Align = UiAlignStyle.None;
            Panel.SlideTo(new Vector2(100, Manager.NativeViewport.Height), 500, EaseFunction.OutQuad);
            // call base hide after the animation has finished
            Panel.FadeTo(0, 1000).StopAnimation += (s) =>
            {
                base.PerformClose();
                base.OnCloseAfter();
            };
        }
    }
}
